package dhm.com.dhmshop.updata.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.MainVpFgAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.GetCollectionShop;
import dhm.com.dhmshop.entity.GetMyshop;
import dhm.com.dhmshop.entity.GetShopDetailBean;
import dhm.com.dhmshop.fragment.wode.shop.ShopFregment;
import dhm.com.dhmshop.framework.module.home.activity.SearchActivity;
import dhm.com.dhmshop.updata.fragment.ShopDetailAllFragment;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.view.main.MainActivity;

public class ShopdetailActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.shop_image)
    SimpleDraweeView shopImage;
    @BindView(R.id.shop_name)
    TextView shopName;
    @BindView(R.id.addr)
    TextView addr;
    @BindView(R.id.shopcheckimage)
    ImageView shopcheckimage;
    @BindView(R.id.shopchecktext)
    TextView shopchecktext;
    @BindView(R.id.shopcheck)
    LinearLayout shopcheck;
    @BindView(R.id.main_tablayout)
    TabLayout mainTablayout;
    @BindView(R.id.main_vp)
    ViewPager mainVp;
    private PressenterImpl pressenter;
    private ArrayList<String> titles;
    private ArrayList<Fragment> fragments;
    private MainVpFgAdapter mainVpFgAdapter;
    private ShopFregment shopFregment;
    private ShopDetailAllFragment shopFregment1;
    private String shop_id;
    private GetShopDetailBean getShopDetailBean;
    private String uid;

    @Override
    protected int getLayout() {
        return R.layout.activity_shopdetail2;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(this, "uid");
        shop_id = getIntent().getStringExtra("shop_id");

        mainTablayout.setSelectedTabIndicatorHeight(0);
        //创建tab
        titles = new ArrayList<>();
        titles.add("首页");
        titles.add("全部商品");

    }

    @Override
    protected void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("shop_id", shop_id);
        pressenter.sendMessage(this, Constant.GetShopInfo, map, GetShopDetailBean.class);
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetShopDetailBean) {
            getShopDetailBean = (GetShopDetailBean) data;
            if (getShopDetailBean.getCode() == 1) {

                shopImage.setImageURI(Constant.PATH+"/"+ getShopDetailBean.getData().getShopImg());
                shopName.setText(getShopDetailBean.getData().getShopName());
                addr.setText(getShopDetailBean.getData().getShopAddress());
                if (getShopDetailBean.getData().getIs_collect()==1){
//                    关注了
                    shopcheck.setBackgroundResource(R.drawable.back_btn);
                    shopchecktext.setText("已收藏");
                }else {
//                    未关注
                    shopcheck.setBackgroundResource(R.drawable.back_shop_liken);
                    shopchecktext.setText("收藏店铺");
                }
                //三个fragments
                fragments = new ArrayList<>();
                shopFregment = new ShopFregment(getShopDetailBean);
                shopFregment1 = new ShopDetailAllFragment(getShopDetailBean);
                fragments.add(shopFregment);
                fragments.add(shopFregment1);
                //设置适配器
                mainVpFgAdapter = new MainVpFgAdapter(getSupportFragmentManager(), fragments, titles);
                changeTextColor(mainTablayout);
                //绑定适配器
                mainVp.setAdapter(mainVpFgAdapter);
                mainVp.setOffscreenPageLimit(0);
                //设置联动
                mainTablayout.setupWithViewPager(mainVp);

                setupTabIcons();//设置底部TabLayout的item
            }
        }else if (data instanceof GetCollectionShop) {
            GetCollectionShop getCollectionShop = (GetCollectionShop) data;
            if (getCollectionShop.getCode() == 1) {
                for (int i = 0; i < getCollectionShop.getData().size(); i++) {
                    if (shop_id.equals(getCollectionShop.getData().get(i).getShopId() + "")) {
                            Map<String, String> map = new HashMap<>();
                            map.put("token", Constant.TOKEN);
                            map.put("uid", uid);
                            map.put("type", "1");
                            map.put("cid", getCollectionShop.getData().get(i).getFavoriteId() + "");
                            pressenter.sendMessage(ShopdetailActivity.this, Constant.DelShopCollect, map, Bean.class);
                        i=getCollectionShop.getData().size();
                    } else {
                        shopcheck.setBackgroundResource(R.drawable.back_shop_liken);
                        shopchecktext.setText("收藏店铺");
                    }
                }

            }
        } else if (data instanceof Bean) {
            Bean bean = (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode() == 1) {
                if (bean.getMessage().equals("关注成功")) {
                    getShopDetailBean.getData().setIs_collect(1);
                } else {
                    getShopDetailBean.getData().setIs_collect(0);
                }
                if (getShopDetailBean.getData().getIs_collect()==1){
//                    关注了
                    shopcheck.setBackgroundResource(R.drawable.back_btn);
                    shopchecktext.setText("已收藏");
                }else {
//                    未关注
                    shopcheck.setBackgroundResource(R.drawable.back_shop_liken);
                    shopchecktext.setText("收藏店铺");
                }
            }
        }

    }

    private void changeTextColor(TabLayout tabLayout) {
        try {
            //拿到tabLayout的mTabStrip属性
            Field field = TabLayout.class.getDeclaredField("mTabStrip");
            if (field == null){
                return;
            }
            field.setAccessible(true);
            //拿mTabStrip属性里面的值
            Object mTabStrip = field.get(tabLayout);
            //通过mTabStrip对象来获取class类，不能用field来获取class类，参数不能写Integer.class
            Method method = mTabStrip.getClass().getDeclaredMethod("setSelectedIndicatorColor", int.class);
            method.setAccessible(true);
            method.invoke(mTabStrip, Color.parseColor("#ffffff"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //有几个底部的item就写几个
    private void setupTabIcons() {
        //tablayout图文效果
        for (int i = 0; i < fragments.size(); i++) {
            mainTablayout.getTabAt(i).setCustomView(getView(i));
        }
    }

    @SuppressLint("NewApi")
    private View getView(int position) {
        View tabitem = LayoutInflater.from(this).inflate(R.layout.tabshop, null);
        TextView b = tabitem.findViewById(R.id.b);
        TextView tv = tabitem.findViewById(R.id.tabtv);
        tv.setText(titles.get(position));
        b.setBackgroundResource(R.drawable.tab_white);
        return tabitem;


    }

    @Override
    public void fail(String error) {

    }

    @OnClick({R.id.back, R.id.mainsearch_homefg, R.id.shopping, R.id.tab, R.id.shopcheck, R.id.shopdetail})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.mainsearch_homefg:
                intent=new Intent(this, SearchActivity.class);
                intent.putExtra("type","3");
                startActivity(intent);
                break;
            case R.id.shopping:
                intent=new Intent(this, MainActivity.class);
                intent.putExtra("type","1");
                startActivity(intent);
                break;
            case R.id.tab:
                intent=new Intent(this,ShopTypeActivity.class);
                intent.putExtra("shop_id",shop_id);
                startActivity(intent);
                break;
            case R.id.shopcheck:
                if (getShopDetailBean.getData().getIs_collect()==1){
//                    关注了
                    Map<String, String> map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    map.put("type","1");
                    pressenter.sendMessage(this, Constant.MyShopCollects, map, GetCollectionShop.class);
                }else {
//                    未关注
                    Map<String, String> map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid",uid);
                    map.put("id", shop_id);
                    map.put("type","1");
                    pressenter.sendMessage(this, Constant.AddShopCollect, map, Bean.class);
                }
                break;
            case R.id.shopdetail:
                intent=new Intent(this,ShopintroduceActivity.class);
                intent.putExtra("shop_id",shop_id);
                startActivity(intent);
                break;
                default:
        }
    }
}
